﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditHotel : AdminPageBase
    {
        private HotelInfo _hotelInfo = new HotelInfo();
        private readonly HotelController _hotelController = new HotelController();
        protected List<UtilityInfo> UtilityList = new List<UtilityInfo>(); 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["HotelId"]))
                _hotelInfo = _hotelController.GetHotel(int.Parse(Request.QueryString["HotelId"]));

            if (Page.IsPostBack) return;

            LoadPartner();
            LoadContinent();
            LoadAddressDefault();
            LoadHotelType();
            LoadStatus();
            LoadUser();
            UtilityList = new UtilityController().ListUtility().Where(p => p.UtilityType == 1).OrderBy(p => p.UtilityId).ToList();

            if (_hotelInfo.HotelId == -1) return;

            LoadNational();
            LoadCity();
            LoadDistrict();
            LoadWard();

            RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListHotel.aspx?xml=Hotel");
        }

        private void SaveData()
        {
            _hotelInfo.HotelFullName = txtHotelFullName.Text;
            _hotelInfo.HotelSortName = txtHotelSortName.Text;
            _hotelInfo.HotelEnglishName = txtHotelEnglishName.Text;
            _hotelInfo.PartnerId = int.Parse(ddlPartner.SelectedValue);
            _hotelInfo.Address = txtAddress.Text;
            _hotelInfo.Status= int.Parse(ddlStatus.SelectedValue);
            _hotelInfo.Utilities = txtUtilities.Value;

            _hotelInfo.ContinentId = int.Parse(ddlContinent.SelectedValue);
            _hotelInfo.NationalId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlNational"]);
            _hotelInfo.CityId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlCity"]);
            _hotelInfo.DistrictId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlDistrict"]);
            _hotelInfo.WardId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlWard"]);


            _hotelInfo.HotelTypeId = int.Parse(ddlHotelType.SelectedValue);
            _hotelInfo.FloorQuantity = int.Parse(txtFloorQuantity.Text);
            _hotelInfo.SortDescription = txtSortDesription.Text;
            _hotelInfo.DetailDescrtiption = txtContent.Text;
            _hotelInfo.RepresentName = txtRepresentName.Text;
            _hotelInfo.RepresentPhone = txtPhone.Text;
            _hotelInfo.RepresentHotline = txtHotline.Text;
            _hotelInfo.RepresentFax = txtFax.Text;
            _hotelInfo.RepresentEmail = txtRepresentEmail.Text;
            _hotelInfo.RepresentWebsite = txtWebsite.Text;

            _hotelInfo.Item_Type = 5;

            if (_hotelInfo.HotelId == -1)
            {
                _hotelInfo.CreatedBy = ddlCreatedBy.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlCreatedBy.SelectedValue);
                _hotelInfo.CreatedDate = DateTime.Now;
                _hotelController.InsertHotel(_hotelInfo);
            }
            else
            {
                _hotelInfo.UpdatedBy = UserId;
                _hotelInfo.UpdatedDate = DateTime.Now;
                _hotelController.UpdateHotel(_hotelInfo);
            }

            Response.Redirect("/Pages/ListHotel.aspx?xml=Hotel");
        }

        private void RenderForm()
        {
            txtHotelFullName.Text=_hotelInfo.HotelFullName;
            txtHotelSortName.Text=_hotelInfo.HotelSortName;
            txtHotelEnglishName.Text=_hotelInfo.HotelEnglishName;

            txtSortDesription.Text=_hotelInfo.SortDescription;
            txtContent.Text=_hotelInfo.DetailDescrtiption;
            txtRepresentName.Text=_hotelInfo.RepresentName;
            txtPhone.Text=_hotelInfo.RepresentPhone;
            txtHotline.Text=_hotelInfo.RepresentHotline;
            txtFax.Text=_hotelInfo.RepresentFax;
            txtRepresentEmail.Text=_hotelInfo.RepresentEmail;
            txtWebsite.Text=_hotelInfo.RepresentWebsite;
            txtAddress.Text=_hotelInfo.Address;
            txtUtilities.Value = _hotelInfo.Utilities;

            ddlNational.SelectedValue = _hotelInfo.NationalId.ToString();
            ddlCity.SelectedValue = _hotelInfo.CityId.ToString();
            ddlWard.SelectedValue = _hotelInfo.WardId.ToString();
            ddlDistrict.SelectedValue = _hotelInfo.DistrictId.ToString();
            ddlContinent.SelectedValue = _hotelInfo.ContinentId.ToString();
            ddlHotelType.SelectedValue = _hotelInfo.HotelTypeId.ToString();
            ddlStatus.SelectedValue = _hotelInfo.Status.ToString();
            ddlCreatedBy.SelectedValue = _hotelInfo.CreatedBy.ToString();
            txtFloorQuantity.Text = _hotelInfo.FloorQuantity.ToString();
            ddlPartner.SelectedValue = _hotelInfo.PartnerId.ToString();
        }

        private void LoadPartner()
        {
            ddlPartner.DataSource =
               new PartnerController().ListPartner()
                   .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                   .OrderBy(p => p.PartnerName);
            ddlPartner.DataTextField = "PartnerName";
            ddlPartner.DataValueField = "PartnerId";
            ddlPartner.DataBind();

            ddlPartner.Items.Insert(0, new ListItem("--- Chọn đối tác ---", "-1"));
        }

        private void LoadAddressDefault()
        {
            ddlNational.Items.Insert(0, new ListItem("--- Chọn quốc gia ---", "-1"));
            ddlCity.Items.Insert(0, new ListItem("--- Chọn tỉnh/thành phố ---", "-1"));
            ddlDistrict.Items.Insert(0, new ListItem("--- Chọn quận/huyện ---", "-1"));
            ddlWard.Items.Insert(0, new ListItem("--- Chọn phường/xã ---", "-1"));
        }

        private void LoadContinent()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Continent.xml"));
            var dv = ds.Tables["Continent"].DefaultView;
            ddlContinent.DataTextField = "Name";
            ddlContinent.DataValueField = "ID";
            ddlContinent.DataSource = dv;
            ddlContinent.DataBind();

            ddlContinent.Items.Insert(0, new ListItem("--- Chọn châu lục ---", "-1"));
        }
        private void LoadNational()
        {
            ddlNational.DataSource = new NationalController().ListNationalByContinent(_hotelInfo.ContinentId);
            ddlNational.DataTextField = "NationalName";
            ddlNational.DataValueField = "NationalId";
            ddlNational.DataBind();

            ddlNational.Items.Insert(0, new ListItem("--- Chọn quốc gia ---", "-1"));
        }
        private void LoadCity()
        {
            ddlCity.DataSource = new CityController().ListCityByNational(_hotelInfo.NationalId);
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityId";
            ddlCity.DataBind();

            ddlCity.Items.Insert(0, new ListItem("--- Chọn tỉnh/thành phố ---", "-1"));
        }
        private void LoadDistrict()
        {
            ddlDistrict.DataSource = new DistrictController().ListDistrictByCity(_hotelInfo.CityId);
            ddlDistrict.DataTextField = "DistrictName";
            ddlDistrict.DataValueField = "DistrictId";
            ddlDistrict.DataBind();

            ddlDistrict.Items.Insert(0, new ListItem("--- Chọn quận/huyện ---", "-1"));
        }
        private void LoadWard()
        {
            ddlWard.DataSource = new WardController().ListWardByDistrict(_hotelInfo.DistrictId);
            ddlWard.DataTextField = "WardName";
            ddlWard.DataValueField = "WardId";
            ddlWard.DataBind();

            ddlWard.Items.Insert(0, new ListItem("--- Chọn phường/xã ---", "-1"));
        }
        private void LoadHotelType()
        {
            ddlHotelType.DataSource =
               new HotelTypeController().ListHotelType()
                   .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                   .OrderBy(p => p.HotelTypeName);
            ddlHotelType.DataTextField = "HotelTypeName";
            ddlHotelType.DataValueField = "HotelTypeId";
            ddlHotelType.DataBind();

            ddlHotelType.Items.Insert(0, new ListItem("--- Loại khách sạn ---", "-1"));
        }
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
        private void LoadUser()
        {
            ddlCreatedBy.DataSource = new AdminUserController().ListAdminUser();
            ddlCreatedBy.DataTextField = "UserName";
            ddlCreatedBy.DataValueField = "UserId";
            ddlCreatedBy.DataBind();

            ddlCreatedBy.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
    }
}